package org.an.proj.contacts.ui;

import android.Manifest;
import android.os.Bundle;
import com.tbruyelle.rxpermissions.RxPermissions;
import java.io.IOException;
import org.an.ku.util.ACache;
import org.an.ku.util.ToastUtil;
import org.an.proj.contacts.MyApplication;
import org.an.proj.contacts.R;
import org.an.proj.contacts.config.ACacheKey;
import org.an.proj.contacts.model.bean.ConfigInfoVo;
import org.an.proj.contacts.model.response.Result;
import org.an.proj.contacts.net.api.GithubApi;
import org.an.proj.contacts.net.service.retrofit.call.SimpleCallback;
import org.an.proj.contacts.net.service.retrofit.util.ServiceGenerator;
import org.an.proj.contacts.ui.base.BaseActivity;
import org.an.proj.contacts.util.IntentUtil;
import retrofit2.Call;
import rx.functions.Action1;

/**
 * 闪屏页
 */
public class SplashActivity extends BaseActivity {
  private Call<Result<ConfigInfoVo>> mCall;

  @Override protected boolean isFull() {
    return true;
  }

  @Override protected boolean openSwipeBack() {
    return false;
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // 处理通过安装器安装完成之后的“打开”启动app后，home键进入后台，然后通过桌面laucher启动app时重启app的问题（下面两种判断都是可以的）
    //if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
    if (!isTaskRoot()) {
      finish();
      return;
    }
    setContentView(R.layout.activity_splash);
    requestPermission();
  }

  /**
   * 先要申请必要的，且无合适触发点的权限，如READ_PHONE_STATE，如果用户拒绝直接禁止使用app
   */
  private void requestPermission() {
    RxPermissions rxPermissions = new RxPermissions(this);
    // rxPermissions request被允许的话就不用再次申请了
    // 如果拒绝过，下次权限申请的弹窗就会提示（拒绝后不再询问）
    rxPermissions.request(Manifest.permission.READ_PHONE_STATE).subscribe(new Action1<Boolean>() {
      @Override public void call(Boolean granted) {
        if (granted) { // Always true pre-M 23以前一旦安装时被授予，总是返回true
          route();
        } else {
          ToastUtil.toast(MyApplication.getInstance(), "请授予权限");
          finish();
        }
      }
    });
  }

  private void route() {
    IntentUtil.goToOthersF(mContext, MainActivity.class);

    /*if (StoreConfig.getInstance().isLogin()) {
      mIntentFactory.goToOthersF(MainActivity.class);
    } else {
      mIntentFactory.goToOthersF(LoginActivity.class);
    }*/
  }

  private void loadConfig() {
    showLoadingBar();
    mCall = ServiceGenerator.createGithubService(GithubApi.class).getConfigInfos();
    mCall.enqueue(new SimpleCallback<Result<ConfigInfoVo>>() {
      @Override public void networkError(Call<Result<ConfigInfoVo>> call, IOException ex) {
        dismissLoadingBar();
      }

      @Override public void onResponse(Call<Result<ConfigInfoVo>> call, Result<ConfigInfoVo> entity,
          boolean isSuccess) {
        dismissLoadingBar();
        if (isSuccess) {
          if (entity.data != null) {
            ACache.get(MyApplication.getInstance()).put(ACacheKey.TAB, entity);
            IntentUtil.goToOthersF(mContext, MainActivity.class);
          }
        }
      }
    });
  }
}
